#pragma once
#include "Log.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>
#include <strings.h>

extern Log lg;

const int backlog = 10;

enum
{
    SOCKET_ERR = 1,
    BIND_ERR,
    LISTEN_ERR
};

class Sock
{
public:
    Sock()
    {
    }

    ~Sock()
    {
    }

public:
    void Socket()
    {
        listensock_ = socket(AF_INET, SOCK_STREAM, 0);
        if (listensock_ < 0)
        {
            lg(Fatal, "socket error, listensock_:%d", listensock_);
            exit(SOCKET_ERR);
        }
        // lg(Info, "create socket success,listensock_:%d", listensock_);
    }

    void Bind(const uint16_t port)
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(port);
        server.sin_addr.s_addr = INADDR_ANY;

        if (bind(listensock_, (const struct sockaddr *)&server, sizeof(server)) < 0)
        {
            lg(Fatal, "bind error, listensock_:%d", listensock_);
            exit(BIND_ERR);
        }
        // lg(Info, "bind success,listensock_:%d", listensock_);
    }

    void Listen()
    {
        if (listen(listensock_, backlog) < 0)
        {
            lg(Fatal, "listen error, listensock_:%d", listensock_);
            exit(LISTEN_ERR);
        }
        // lg(Info, "listen success,listensock_:%d", listensock_);
    }

    void Close()
    {
        close(listensock_);
    }

    int Getfd()
    {
        return listensock_;
    }

public:
    int Accept(std::string *clientip, uint16_t *clientport)
    {
        struct sockaddr_in client;
        memset(&client, 0, sizeof(client));
        socklen_t len = sizeof(client);

        int newsock = accept(listensock_, (struct sockaddr *)&client, &len);
        if (newsock < 0)
        {
            lg(Warning, "accept error, sockfd:%d, error string:%s", newsock, strerror(errno));
            return -1;
        }

        // 将客户端的ip和port传出去
        char ipbuffer[64];
        inet_ntop(AF_INET, (const struct sockaddr *)&client.sin_addr, ipbuffer, sizeof(ipbuffer));
        *clientip = ipbuffer;
        *clientport = ntohs(client.sin_port);

        lg(Info, "accept a new link, sockfd: %d, clientip: %s, clientport: %d", newsock, clientip->c_str(), *clientport);
        return newsock;
    }

    bool Connect(const std::string &serverip, const uint16_t serverport)
    {
        struct sockaddr_in server;
        bzero(&server, 0);
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(serverip.c_str());
        server.sin_port = htons(serverport);

        if (connect(listensock_, (const struct sockaddr *)&server, sizeof(server)) < 0)
        {
            // lg(Warning, "connect error, sockfd:%d, error string:%s", listensock_, strerror(errno));
            std::cerr << "connect error, sockfd: " << listensock_ << ", error string: " << strerror(errno) << std::endl;
            return false;
        }

        return true;
    }

private:
    int listensock_;
};